package com.wangzaiplus.test.util;

/**
 * @ClassName SellTicketTest3
 * @Description: TODO
 * @Author wgc
 * @Date 2020/04/08 14:20
 * @Version V1.0
 **/
class TicketWindow3 implements Runnable{
    private static int ticket = 30;
    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (ticket > 0) {
                    try {
                        // 线程出现阻塞
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket-- + "张票，" + "剩余" + ticket + "张票");
                } else {
                    System.out.println(Thread.currentThread().getName() + "余票不足,停止售票!");
                    break;
                }
            }
        }
    }
}

public class SellTicketTest3 {
    public static void main(String[] args) {
        TicketWindow3 tw = new TicketWindow3();

        for (int i = 1; i < 10; i++) {
            Thread t = new Thread(tw,"窗口"+i);
            t.start();
        }
    }
}

